<?php
require_once("../../../util/configuration/configuration_parametters.php");
require_once("../../../util/" . MESSAGES_PROPERTIES . "messages_model.php");
require_once("../../../util/exception/exception_handler.php");


require_once("../../article/to/article_to.php");
require_once("../../article/dao/article_dao_factory.php");

require_once("../../autor/to/autor_to.php");
require_once("../../autor/dao/autor_dao_factory.php");


class Article_dao_test {
	
	private $dao;
	private $autor;

	public function __construct() {		
	}
	
	public function before_test() {
		$this->dao = Autor_dao_factory::singleton()->get_dao();
		$this->autor = new Autor_to('xxxxxxxxxxxx','password','email');
		$this->dao->create($this->autor);
		$this->dao = Article_dao_factory::singleton()->get_dao(); 	
	}
 
	
	public function run_test() {
		try {
			$date = date("Y-m-d H:i:s");
			$article_to = new Article_to(0, 'title', $this->autor->get_login(), $date, 'open', 0);	
			
			$article_to = (object) $this->dao->create($article_to);	
			echo 'create<br/>';
			
			echo 'article_to: ';
			echo $article_to->to_string();
			echo '<br/>';
			
			if ($this->dao->exist($article_to->get_number())) {
				$result = $this->dao->find_by_number_article($article_to->get_number());
				echo 'find<br/>';
				
				$aux_article_to = new Article_to($result['number'], $result['title'], 
													$result['autor_login'], $result['date'],
													$result['state'], $result['view']);
				
				if ($article_to->equals($aux_article_to)) {
					$article_to->set_title("title2");
					$this->dao->update($article_to);
					echo 'update<br/>';
					
					$result = $this->dao->find_by_number_article($article_to->get_number());
					$aux_article_to = new Article_to($result['number'], $result['title'], 
													$result['autor_login'], $result['date'],
													$result['state'], $result['view']);
					echo 'article_to: ';
					echo $aux_article_to->to_string();
					echo '<br/>';
				}
				
				$this->dao->remove($article_to->get_number());
				echo 'remove<br/>';			

			}
		}
		catch (Exception $e) {
			Exception_handler::singleton()->handler($e);
		}
	}
	
	function after_test() {
		Autor_dao_factory::singleton()->get_dao()->remove($this->autor->get_login());
	}
}



//Test
$test = new Article_dao_test();

$test->before_test();
$test->run_test();
$test->after_test();
							

?>